package com.codeoss.framework.view.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.Tag;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.service.IVariableService;

@Service
public class TagComponentService extends ComponentService {
	@Autowired
    private IVariableService variableService;
	@Autowired
	private IConverterStore converterStore;
	@Autowired
	private ITableStore tableStore;
	
	@Override
	public void processValue(YunContext yunContext, Component component,Map<String,String> paraMap,Map<String,Object> dataMap) {
		// TODO Auto-generated method stub
		Tag tagComponent = (Tag) component;
		String dataType=tagComponent.getDataType();
		if(StringUtils.isEmpty(dataType)) {
			Page page=yunContext.getPage();
			DataSourceConfig dataSourceConfig=page.getDataSourceConfigById(tagComponent.getDataSource());
			Table table=tableStore.get(dataSourceConfig.getTableName());
			Column column=table.getColumnByColumnName(tagComponent.getDataField());
			dataType=column.getDataType();
			tagComponent.setDataType(dataType);
		}
		if(paraMap.containsKey(tagComponent.getId())) {
			String sValue=paraMap.get(tagComponent.getId());	
			IConverter converter=converterStore.getConverter(dataType);
			Object value=converter.convertStringToObject(sValue);
			variableService.setVariableValue(yunContext, tagComponent.getVarName(), value);
		}
	}
}
